﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Media;

namespace Projeto3D
{
    static class SoundManager
    {
        public static Dictionary<String, Song> musics = new Dictionary<String, Song>();
        public static Dictionary<String, SoundEffect> sounds = new Dictionary<String, SoundEffect>();
        public static Dictionary<String, SoundEffectInstance> soundsInstances = new Dictionary<String, SoundEffectInstance>();

        static public void addMusic(String musicKey, Song music)
        {
            musics.Add(musicKey, music);

        }

        static public void addSound(String soundKey, SoundEffect sound)
        {
            sounds.Add(soundKey, sound);
        }

        static public void playMusic(String musicKey, Boolean repeat)
        {
            MediaPlayer.IsRepeating = repeat;
            MediaPlayer.Play(musics[musicKey]);
        }

        static public void pauseMusic()
        {
            MediaPlayer.Pause();
        }

        static public void resumeMusic()
        {
            MediaPlayer.Resume();
        }

        static public Boolean isMusicPaused()
        {
            return MediaPlayer.State.Equals(MediaState.Paused);
        }

        static public void playSound(String soundKey)
        {
            getSoundInstance(soundKey).Play();
        }

        static public void playSound(String soundKey, float volume)
        {
            SoundEffectInstance instance = getSoundInstance(soundKey);
            instance.Volume = volume;
            instance.Play();
        }

        static public void playSound(String soundKey, bool loop)
        {
            SoundEffectInstance instance = getSoundInstance(soundKey);
            instance.IsLooped = loop;
            instance.Play();
        }

        static public void pauseSound(String soundKey)
        {
            SoundEffectInstance instance = getSoundInstance(soundKey);

            if (instance.State == SoundState.Playing)
                instance.Pause();
        }

        static public void resumeSound(String soundKey)
        {
            SoundEffectInstance instance = getSoundInstance(soundKey);

            if (instance.State == SoundState.Paused)
                instance.Resume();
        }

        static public void stopMusic()
        {
            MediaPlayer.Stop();
        }

        static public void stopSound(String soundKey, bool immediate)
        {
            getSoundInstance(soundKey).Stop(immediate);
            soundsInstances.Remove(soundKey);
        }

        static public void volumeMusicAdjust(float volume)
        {
            MediaPlayer.Volume += volume;
        }

        static public SoundEffectInstance getSoundInstance(String soundKey)
        {
            if (!soundsInstances.ContainsKey(soundKey))
                soundsInstances.Add(soundKey, sounds[soundKey].CreateInstance());

            return soundsInstances[soundKey];
        }
    }
}
